package com.qf.flash.ui.home;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.event.PayResetEvent;
import com.qf.flash.http.response.JoinTeamBean;
import com.qf.flash.http.response.PayResetResponse;
import com.qf.flash.http.response.TipsBean;
import com.qf.flash.model.Pay2JoinBean;
import com.qf.flash.model.Pay2SponsorBean;
import com.qf.flash.model.PayParam;
import com.qf.flash.model.PayResetBean;
import com.qf.flash.mvp.contract.PayContract;
import com.qf.flash.mvp.contract.PayParamContract;
import com.qf.flash.mvp.presenter.PayParamPresenter;
import com.qf.flash.mvp.presenter.PayPresenterImpl;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.config.Constants;
import com.qf.flashlib.sharedpreferences.SavePermanentSharePrefs;
import com.qf.flashlib.sharedpreferences.SharePrefConstants;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 2020/2/27 9:29
 */
public class OrderPayActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener,
        PayParamContract.View, PayContract.View {

    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_djd)
    TextView tvDjd;
    @BindView(R.id.rb_djd)
    CheckBox rbDjd;
    @BindView(R.id.rb_wx)
    CheckBox rbWx;
    @BindView(R.id.rb_zfb)
    CheckBox rbZfb;


    private PayParamPresenter payParamPresenter;
    private PayPresenterImpl mPayPresenter;
    private Pay2JoinBean pay2JoinBean;
    private Pay2SponsorBean pay2SponsorBean;
    private PayResetBean payResetBean;
    private String teamId;

    public static void start(Context activity, Pay2SponsorBean sponsorBean) {
        Intent intent = new Intent(activity, OrderPayActivity.class);
        intent.putExtra("data", sponsorBean);
        activity.startActivity(intent);
    }

    public static void start(Context activity, Pay2JoinBean joinBean) {
        Intent intent = new Intent(activity, OrderPayActivity.class);
        intent.putExtra("data", joinBean);
        activity.startActivity(intent);
    }
    public static void start(Context activity, PayResetBean payResetBean) {
        Intent intent = new Intent(activity, OrderPayActivity.class);
        intent.putExtra("data", payResetBean);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_pay;
    }

    @Override
    protected void initInject() {
        payParamPresenter = new PayParamPresenter();
        mPayPresenter = new PayPresenterImpl(this);
        initPresenters(payParamPresenter, mPayPresenter);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("订单支付");
        initRadioButtonListener();

        Object obj = getIntent().getParcelableExtra("data");
        if (obj instanceof Pay2JoinBean) {
            pay2JoinBean = (Pay2JoinBean) obj;
            tv_money.setText(String.format("¥%s", pay2JoinBean.getMoney()));
        } else if (obj instanceof Pay2SponsorBean) {
            pay2SponsorBean = (Pay2SponsorBean) obj;
            tv_money.setText(String.format("¥%s", pay2SponsorBean.getMoney()));
        } else if (obj instanceof PayResetBean) {
            payResetBean = (PayResetBean) obj;
            tv_money.setText(String.format("¥%s", payResetBean.getMoney()));
        }

//        private String userId;  //	是	String	当前登录用户id
//        private String payType; //	否	String	支付方式，0代金点，1微信，2支付宝，3代金点+微信，4代金点+支付宝，免费时可不传
//        private String teamName;//	是	String	队名
//        private String startTime;//	是	String	游戏时间
//        private String password;//	否	String	密码
//        private int num;        //	否	Int	人数
//        private String playId;  //	否	String	剧本id
//        private String playType;//	是	Integer	游戏类型id
//        private double money;   //	否	Integer	费用，免费传0

        payParamPresenter.getPayTips();

    }

    //发起组队
    private void getSponsorParam(Pay2SponsorBean bean, int payType) {
        if (bean == null) return;
        String areaId = SavePermanentSharePrefs.getInstance().getDataString(SharePrefConstants.AREA_ID);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", UserMgr.getInstance().getLoginUser().getUid());
        map.put("payType", payType);
        map.put("startTime", bean.getStartTime());
        map.put("teamName", bean.getTeamName());
        map.put("password", bean.getPassword());
        map.put("num", bean.getNum());
        map.put("playId", bean.getPlayId());
        map.put("playType", bean.getPlayType());
        map.put("money", bean.getMoney());
        map.put("areaId", areaId);
        map.put("isShopEnter", bean.getIsShopEnter());
        payParamPresenter.getStartTeamParam(map);
    }
    //加入组队
    private void getJoinParam(Pay2JoinBean bean, int payType) {
        if (bean == null) return;
        payParamPresenter.getJoinTeamParam(bean.getOrderId(), payType);
    }


    private List<CheckBox> radioGroup = new ArrayList<>();

    private void initRadioButtonListener() {
        radioGroup.add(rbWx);
        radioGroup.add(rbZfb);
        rbWx.setOnCheckedChangeListener(this);
        rbZfb.setOnCheckedChangeListener(this);
    }

    private void changePayWay(int rbId) {
        for (CheckBox radioButton : radioGroup) {
            radioButton.setChecked(rbId == radioButton.getId());
        }
    }

    /**
     * 获取支付类型
     */
    private int checkPayWay() {
        if (rbZfb.isChecked()) {
            if (rbDjd.isChecked()) {
                return Constants.PAY_D_ALI;  //代金点 + 支付宝
            }
            return Constants.PAY_ALI;
        }

        if (rbWx.isChecked()) {
            if (rbDjd.isChecked()) {
                return Constants.PAY_D_WX;//代金点 + 微信
            }

            return Constants.PAY_WX;
        }
        if (rbDjd.isChecked()) {
            return Constants.PAY_DJD;
        }
        return -1;

    }

    @OnClick({R.id.tv_top_back, R.id.tv_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_pay:
                onPayClick();
                break;
        }
    }

    private void onPayClick() {
        int payWay = checkPayWay();
        if (payWay == -1) {
            FlashLib.showToast("请选择支付方式");
            return;
        }
        if (pay2SponsorBean != null) {
            getSponsorParam(pay2SponsorBean, payWay);
        }

        if (pay2JoinBean != null) {
            getJoinParam(pay2JoinBean, payWay);
        }

        if (payResetBean != null) {
            payResetBean.setPayType(payWay);
            payParamPresenter.payReset(payResetBean.getUserId(), payResetBean.getOrderId(), payWay);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked)
            changePayWay(buttonView.getId());
    }

    //发起组队付钱
    @Override
    public void getStParamSuccess(PayParam payParam) {
        teamId = payParam.getOrderId(); // 订单id
        if (TextUtils.equals("1", payParam.getPayState())) {
            onPaySuccess();
        } else if (rbWx.isChecked())
            mPayPresenter.showPay(Constants.PAY_WX, payParam.getThirdPayParams());
        else
            mPayPresenter.showPay(Constants.PAY_ALI, payParam.getThirdPayParams());
    }
    //加入组队付钱
    @Override
    public void getJoinParamSuccess(JoinTeamBean joinTeamBean) {
        teamId = joinTeamBean.getOrderId();
        if (TextUtils.equals("1", joinTeamBean.getPayState())) {
            onPaySuccess();
        } else if (rbWx.isChecked())
            mPayPresenter.showPay(Constants.PAY_WX, joinTeamBean.getThirdPayParams());
        else
            mPayPresenter.showPay(Constants.PAY_ALI, joinTeamBean.getThirdPayParams());
    }

    @Override
    public void setOrderTips(TipsBean bean) {
        if (bean == null)
            return;
        tvTips.setText("温馨提示： " + bean.getTip());
        tvDjd.setText(String.format("账号代金点(%s)", bean.getPoint()));
    }

    @Override
    public void payResetSuccess(PayResetResponse resetResponse) {
        teamId = resetResponse.getOrderId();
        if (TextUtils.equals("1", resetResponse.getPayState())) {
            onPaySuccess();
        } else if (rbWx.isChecked())
            mPayPresenter.showPay(Constants.PAY_WX, resetResponse.getThirdPayParams());
        else
            mPayPresenter.showPay(Constants.PAY_ALI, resetResponse.getThirdPayParams());
    }

    @Override
    public void onPaySuccess() {
        FlashLib.showToast("支付成功");
        if (payResetBean != null) {
            EventBus.getDefault().post(new PayResetEvent());
            finish();
        } else {
            OrderPaySuccessActivity.start(this, teamId, pay2SponsorBean != null);
        }

    }

    @Override
    public void onPayFailed() {
        FlashLib.showToast("支付失败");
    }
}
